<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>构造继承</title>
</head>
<body>
    <script>
        // 构造继承不考虑原型
        function Animal (name, age) {
            this.name = name
            this.age = age
            this.eat = function (food) {
                console.log(this.name + ' is eating ' + food)
            }
        }
        Animal.prototype.say = function () {
            console.log('My name is ' + this.name)
        }
        // 把父类的构造函数当成普通函数在子类的构造函数里调用，并且修改this指向
        function Cat (name, age) {
            // 这里Animal作为普通的全局函数再调用，所以里面的this指window
            // 所以希望在调用Animal的时候把this指向外层Dog的this
            // call或者apply
            // Animal.call(this, name, age)
            Animal.apply(this, [ name, age ])
        }

        var tom = new Cat('Tom', 100)
        console.log(tom)
        // tom.say()  // 报错，继承不了原型上的方法
        tom.eat('Jerry')

        var coffee = new Cat('Coffee')
        coffee.eat('fish')

        console.log(coffee.eat === tom.eat) // false

        console.log(tom instanceof Cat) // true
        console.log(tom instanceof Animal) // false


    </script>
</body>
</html>